package com.uchedao.vehicledetection.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.model.PaintThickness;
import com.uchedao.vehicledetection.view.widget.stickylistheaders.StickyListHeadersAdapter;

import java.text.DecimalFormat;

public class PaintThicknessAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    public interface PaintThicknessListener {
        void onClick(int position);
    }

    // private Context mContext;
    private PaintThickness[] paintThicknessArray;
    private LayoutInflater inflater;
    private String[] values;
    private PaintThicknessListener listener;

    public PaintThicknessAdapter(Context context, PaintThicknessListener paintThicknessListener) {
        inflater = LayoutInflater.from(context);
        listener = paintThicknessListener;
//		paintThicknessArray = result;
        values = GlobalInfo.resources.getStringArray(R.array.paint_result_entries);
    }

    public void setData(PaintThickness[] result) {
        this.paintThicknessArray = result;
        notifyDataSetChanged();
    }

    public void setData(PaintThickness paintThickness) {
        for (int i = 0; i < paintThicknessArray.length; i++) {
            if (paintThicknessArray[i].name.equals(paintThickness.name)) {
                paintThicknessArray[i] = paintThickness;
                notifyDataSetChanged();
                return;
            }
        }

    }

    @Override
    public int getCount() {
        return paintThicknessArray.length;
    }

    @Override
    public Object getItem(int position) {
        return paintThicknessArray[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        LinearLayout subjectLayout;
        //		ImageView itemIv;// 完成图标
        TextView titleTv;
        TextView pointsTv;// 点数
        //		TextView valueTv;
        TextView resultTv;
//		TextView reTestTv;// 重新检测
//		TextView showDetailTv;// 查看详情

    }

    private OnClickListener resetListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            // paintThicknessArray[position].result.clear();
            for (int i = 0; i < paintThicknessArray[position].result.size(); i++) {
                paintThicknessArray[position].result.set(i, -1);
            }
            notifyDataSetChanged();
            if (listener != null)
                listener.onClick(position);

        }
    };

    private OnClickListener onClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = (Integer) v.getTag(R.id.subject_layout);
            if (listener != null)
                listener.onClick(position);

        }
    };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.accident_paint_item, null);
            convertView.setTag(viewHolder);
            // viewHolder.subjectLayout = (LinearLayout) convertView
            // .findViewById(R.id.subject_layout);
//			viewHolder.itemIv = (ImageView) convertView.findViewById(R.id.paint_item_iv);
//			viewHolder.itemIv.setVisibility(View.INVISIBLE);
            viewHolder.titleTv = (TextView) convertView.findViewById(R.id.paint_item_title_tv);
            viewHolder.pointsTv = (TextView) convertView.findViewById(R.id.paint_item_points_tv);
//			viewHolder.valueTv = (TextView) convertView.findViewById(R.id.paint_item_value_tv);
            viewHolder.resultTv = (TextView) convertView.findViewById(R.id.paint_item_result_tv);
//			viewHolder.reTestTv = (TextView) convertView.findViewById(R.id.paint_item_reTest_tv);
//			viewHolder.showDetailTv = (TextView) convertView.findViewById(R.id.paint_item_showDetail_tv);

            convertView.setOnClickListener(onClick);
//			viewHolder.reTestTv.setOnClickListener(resetListener);
//			 viewHolder.showDetailTv.setOnClickListener(showDetail);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        PaintThickness result = paintThicknessArray[position];

//		viewHolder.reTestTv.setTag(position);
        convertView.setTag(R.id.subject_layout, position);
        // viewHolder.showDetailTv.setTag(result);
        // viewHolder.subjectLayout.setTag(result);

        viewHolder.titleTv.setText(result.name);

        int m = result.max;
//        for (int k : result.result) {
//            if (k > m)
//                m = k;
//        }

//		viewHolder.valueTv.setText(m == -1 ? "" : String.valueOf(m));
        viewHolder.pointsTv.setText(m == -1 ? m + "" : df.format(((float) m) / 10.0f));
        if (m >= 1500) {
            viewHolder.resultTv.setText(m >= 2500 ? values[2] : values[1]);
            viewHolder.resultTv.setBackgroundResource(R.color.deep_red);
            viewHolder.resultTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_red_cross, 0, 0, 0);
        } else {
            viewHolder.resultTv.setText(values[0]);
            viewHolder.resultTv.setBackgroundResource(R.color.deep_green);
            viewHolder.resultTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_blue_yes, 0, 0, 0);
        }

        return convertView;
    }

    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.accident_paint_subject, null);
        }

        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        return 0;
    }

    DecimalFormat df = new DecimalFormat("####.0");
}
